﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data;

public partial class assets_controls_Menubar : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        using (DataSet ds = new DataSet())
        {
            ds.ReadXml(string.Format(@"{0}\assets\xml\Menus.xml",Request.PhysicalApplicationPath));
            rptMenubar.DataSource = ds.Tables[0];
            rptMenubar.DataBind();
        }
        lnkBtnLogin.Text = (Request.IsAuthenticated) ? "Logout" : "Login";
    }
    protected void rptMenubar_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        DataRowView drv = (DataRowView)e.Item.DataItem;
        string sUrl = Convert.ToString(drv["Url"]);
        using (HyperLink lnk = (HyperLink)e.Item.FindControl("lnkMenu"))
        {
            lnk.Text = Convert.ToString(drv["Title"]);
            lnk.NavigateUrl = (sUrl != "#") ? string.Format("{0}{1}",Context.Items["AppPath"], sUrl) : string.Format("{0}#", Request.Path);
        }
        using (DataView dv = drv.CreateChildView(drv.DataView.Table.DataSet.Relations[0]))
        {
            using (Repeater rpt = (Repeater)e.Item.FindControl("rptSub"))
            {
                rpt.Visible = (dv.Count > 0);
                if(rpt.Visible){
                    rpt.DataSource = dv;
                    rpt.DataBind();
                }
            }
        }
        if (e.Item.ItemIndex == 0)
        {
            HtmlGenericControl oLi = (HtmlGenericControl)e.Item.FindControl("liMenuNav");
            oLi.Attributes.Add("class", "selected");
        }
    }
    protected void rptSub_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        DataRowView drv = (DataRowView)e.Item.DataItem;
        using (HyperLink lnk = (HyperLink)e.Item.FindControl("lnkSubMenu"))
        {
            string sUrl=Convert.ToString(drv["Url"]);
            lnk.Text = Convert.ToString(drv["Title"]);
            lnk.NavigateUrl = (sUrl != "#") ? string.Format("{0}{1}", Context.Items["AppPath"], sUrl) : string.Format("{0}#", Request.Path);
            
        }
    }
    protected void login_Click(object sender, EventArgs e)
    {
        bool forceLogout = false;
        using (LinkButton lnkBtn = (LinkButton)sender)
        {
            forceLogout = (lnkBtn.Text == "Logout");
        }
        if (forceLogout)
        {
            CustomSecurity.CustomAuthentication.Signout();
            Response.Redirect(Context.Items["AppPath"].ToString());
        }
    }
    protected void lnkBtnAccountDetail_Click(object sender, EventArgs e)
    {
        Response.Redirect(string.Format("{0}/AccountSettings.aspx",Context.Items["AppPath"]));
    }
}
